473,433 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,433 software developers and data experts.

turn quotes into " and apostrophes into '


Hi,

I'm looking for a javascript function that will convert input such as this:

<CLUB Code="

into this:

&lt;CLUB Code=&quot;
First, I thought the "escape" function would do it, but that does
something different.

The reason I want such a function is that we're using msxml's
xmlhttprequest to send xml , and for some reason the stuff inside the
soap envelope neds to be quoted as above.

Do I need to write this function myself (I'll probably use "regular
expressions" if I write it myself) or is there an existing function I
can call ?

Thanks. er*******@rcn.com 4/12/04

Jul 23 '05 #1
2 20474
Eric Osman wrote:
I'm looking for a javascript function that will convert input such as
this:

<CLUB Code="

into this:

&lt;CLUB Code=&quot;

<snip>

An example of such a function can be found at:-

<URL: http://www.crockford.com/javascript/remedial.html >

Richard.
Jul 23 '05 #2


Richard Cornford wrote:
Eric Osman wrote:
I'm looking for a javascript function that will convert input such as
this:

<CLUB Code="

into this:

&lt;CLUB Code=&quot;


<snip>

An example of such a function can be found at:-

<URL: http://www.crockford.com/javascript/remedial.html >

Richard.

THanks Richard,

The routines I wrote look like this:

//+-------------------------------------------------------------
//| quoteXml puts appropriate quoting around various elements so
//| that was interpretable xml (or html) code will now be
//| displayable.
//| A new string is returned.
//+-------------------------------------------------------------
function quoteXml (xmlStr) {
var result = new EditableString(xmlStr);
result = result.replaceAll("<", "&lt;");
result = result.replaceAll(">", "&gt;");
result = result.replaceAll("\"", "&quot;");
result = result.replaceAll("'", "&apos;");
return result.data;
}

//+-------------------------------------------------------------
//| unquoteXml removes the quote marks, which is useful when
//| the returned stuff wants to be parsed as xml.
//+-------------------------------------------------------------
function unquoteXml (xmlStr) {
var result = new EditableString(xmlStr);
result = result.replaceAll("&lt;", "<");
result = result.replaceAll("&gt;", ">");
result = result.replaceAll("&quot;", "\"");
result = result.replaceAll("&apos;", "'");
return result.data;
}

Of course, to use them, you need my EditableString object definition too:

//+-------------------------------------------------------------
//| Object type editableString is a string that can be edited with
//| a number of useful methods contained below.
//+-------------------------------------------------------------
function EditableString(str) {
this.data = str;
}

//+-------------------------------------------------------------
//| replaceAll replaces all source strings with destination strings,
//| returning a new EditableString containing the result.
//+-------------------------------------------------------------
EditableString.prototype.replaceAll = function (srcStr, dstStr) {
this.pat = new RegExp(srcStr,"g");
var newStr = this.data.replace (this.pat, dstStr);
return new EditableString(newStr);
}
p.s. I often use an html "<textarea>" in which to display html that I
want to display for the person to see, so they angle brackets and
tags can all be seen.

However, I discovered that this is fine until you try to display
something like "&guot;" in a textarea. When you attempt that,
the browser (well, ie6 anyway) changes it back into a quote mark!

So, in order to make sure "&quot;" gets properly displayed, I
ended up having to change the ampersand to "&amp;" , so for
example, if var requestBody has some stuff in it that I wasnt to
display in a textarea, and that stuff might have "&quot;" in it,
I further quote it like this:

new EditableString(requestBody).replaceAll("&","&amp;" ).data)

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: C. Titus Brown | last post by:
Hi all, while playing with PBP/mechanize/ClientForm, I ran into a problem with the way htmllib.HTMLParser was handling encoded tag attributes. Specifically, the following HTML was not being...
12
by: Sammy | last post by:
Hi, my mind is going crazy. I have tried everything I can think of to no avail. I have tried Disable Output Escaping. I tried to think of a way of enclosing the attribute data in a CDATA...
2
by: Joe Gass | last post by:
I have an xml doc <sections> <section> <title>entry image</title> </section> <section> <title>22'2</title> </section> </sections>
4
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following...
2
by: Johnny Ljunggren | last post by:
Hello all Just noticed that a testprogram I've made creates a non-conforming XML file. Here's the code (C#) and output: XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent =...
2
by: Zyronne | last post by:
Hello.. How do i modify my stored proc that contains ...FOR XML AUTO to generate an xml output that will return myValue's instead of myValue&apos;s? Is it possible? I am finding ways...
1
by: srinivasreddypn | last post by:
Hi everybody, can anybody guide me how to display a special characters like &quot;, &apos; in xml file into HTML page. Any help would be greatly appreciated. Thanks, srinivasreddy
3
by: katieh | last post by:
My website is made in flash. I have a dynamic image gallery within. How flash knows what to load into my gallery is it refers to my .xml document. Within flash, it displays the picture, and then in...
3
by: LionelAndJen | last post by:
I have an XML file that has a free form comment field in which the data provider, very kindly, already uses "&quot;" when writing "doesn't", I have doesn&apos;t . it's PERFECT, because that xml is...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.